<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>
</head>
<body>
    <button>请求数据</button>
    <button>取消请求</button>
</body>
<script type="text/javascript">
    const btns = document.getElementsByTagName("button");
    let cancel = null;

    btns[0].onclick = function (){
        if (cancel != null){
            cancel();
        }

        axios({
            method:"GET",
            url:"http://localhost:3000/posts",
            cancelToken:new axios.CancelToken(function (e){
                    cancel = e;
            })
        }).then(response =>{
            console.log(response);
            cancel = null;
        })
    }

    btns[1].onclick = function (){
        if (cancel != null){
            cancel();
        }
    }
</script>
</html>